package com.kyzh.core.pager.weal.sign

import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.text.Html
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.core.beans.SignV3
import com.gushenge.core.impls.WealImpl
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.requests.WealRequest
import com.kyzh.core.R
import com.gushenge.core.base.activity.BaseActivity
import com.kyzh.core.dialog.showSignOpenVipDialog
import com.kyzh.core.dialog.showSignResignDialog
import com.kyzh.core.dialog.showSignRuleDialog
import com.kyzh.core.utils.*
import kotlinx.android.synthetic.main.activity_sign_v3.*
import org.jetbrains.anko.toast
import java.text.SimpleDateFormat
import java.util.*
import com.gushenge.core.toast
import com.kyzh.core.pager.weal.vip.OpenVipActivity

class SignActivity : BaseActivity() {
    private lateinit var info :SignV3.My
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sign_v3)
    }

    override fun onResume() {
        super.onResume()
        initData()
    }
    private fun initData() {
        WealRequest.getSignInfo {
            info = my
            v3.setVisibility(top.money !="0")
            v6.setVisibility(top.money =="0")
            tv4.text = top.title1
            tv5.text = top.title2
            tvPoint.text = my.charge_points
            tv7.text = Html.fromHtml(String.format(getString(R.string.checkindays),my.serial))
            tvMonth.text = String.format(getString(R.string.sMonth),month)
            tvYear.text = String.format(getString(R.string.sYear),year)
            rvDays.apply {
                layoutManager = GridLayoutManager(this@SignActivity,7)
                val beans = arrayListOf<SignV3.Data>()
                beans.addAll(list)
                adapter = Adapter(R.layout.item_signv3_day,beans)
            }

            tvSign.setOnClickListener {
                WealImpl.sign(object : ResultListener {
                    override fun success(message: Any) {
                        toast(message as String)
                        initData()
                    }

                    override fun error(error: String) {
                        toast(error)
                    }
                })
            }
            tvSignRule.setOnClickListener {
                WealRequest.signRule {

                    showSignRuleDialog(this) {
                        WealImpl.sign(object : ResultListener {
                            override fun success(message: Any) {
                                toast(message as String)
                                initData()
                            }

                            override fun error(error: String) {
                                toast(error)
                            }
                        })
                    }
                }
            }
        }
    }


    private inner class Adapter(layout:Int,beans:ArrayList<SignV3.Data>):BaseQuickAdapter<SignV3.Data,BaseViewHolder>(layout,beans){

        override fun convert(holder: BaseViewHolder, item: SignV3.Data) {
            val thisDay  = SimpleDateFormat("dd").format(Date(System.currentTimeMillis())).toInt()
            holder.setText(R.id.tvDay,item.t.toString())
            holder.setText(R.id.tvStatus,
                            if (item.t > thisDay){
                                ""
                            }else{
                                if (item.sign =="1"){
                                    getString(R.string.checkIn)
                                }else{
                                    getString(R.string.notCheckIn)
                                }
                            })
                    .setTextColorRes(R.id.tvDay,if (item.sign =="1") R.color.white else R.color.font_33 )
                    .setTextColorRes(R.id.tvStatus,if (item.sign =="1") R.color.white else R.color.font_33 )
            holder.itemView.backgroundTintList = ColorStateList.valueOf(Color.parseColor(if (item.sign =="1")"#F87706" else "#F7F7F7"))
            holder.itemView.setOnClickListener {
                when{
                    item.sign == "1" ->{
                        getString(R.string.checkIn).toast()
                    }
                    item.t > thisDay -> {
                        getString(R.string.timeNotUp).toast()
                    }
                    item.t == thisDay -> WealImpl.sign(object : ResultListener {
                        override fun success(message: Any) {
                            toast(message as String)
                            initData()
                        }

                        override fun error(error: String) {
                            toast(error)
                        }
                    })
                    else -> {
                        if (info.huiyuan==0){
                            showSignOpenVipDialog {
                                OpenVipActivity.start(this@SignActivity)
//                                startActivity<BrowserActivity>(GlobalKeys.LINK to "${Base.BASEURLV3}/?ct=app&ac=huiyuan&app=1&token=${uid}&t=${time}&sign=${httpSign(uid)}")

                            }
                        }else{
                            if(info.bu_num>0){
                               showSignResignDialog(item.time.toString(),info.count,info.bu_num.toString()){
                                   WealImpl.reSign(item.time, object : ResultListener {
                                       override fun success() {
                                           getString(R.string.reissueSuccess).toast()
                                           initData()
                                       }

                                       override fun error(error: String) {
                                           toast(error)
                                       }
                                   })
                               }
                            }else{
                                getString(R.string.haveNoReissueNum).toast()
                            }
                        }
                    }
                }
            }
        }
    }
}